//
//  MainVC.swift
//  kxf
//
//  Created by Mac on 2024/3/9.
//

import Foundation
import MCToast

class MainVC: WHBaseViewVC {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        sendPolicyRequest()
        sendUserCenterRequest()
        initUI()
        sendGetVersionRequest()
        sendGetSampleImgRequest()
    }
    lazy var bannerView : BannerVM = {
        let zhView = BannerVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        zhView.spinChainBlock = {(dict)in
            DLLog(message: "BannerVM:\(dict)")
            
        }
        zhView.isMainHot = true
        zhView.controller = self
        zhView.backgroundColor = .clear
        zhView.setDataSource(dataSourceArr: [])
        
        return zhView
    }()
    lazy var zhanYeFuncVm : MainFuncVM = {
        let vm = MainFuncVM.init(frame: CGRect.init(x: 0, y: self.bannerView.frame.maxY+kFitWidth(10), width: 0, height: 0))
        vm.funcOneVm.tapBlock = {()in
            if UserModel.shared.verifyStatusInt >= 1{
                let vc = AuthenTypeVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }else{
                MCToast.mc_text("请先完成实名认证！")
            }
        }
        vm.funcTwoVm.tapBlock = {()in
            if UserModel.shared.verifyStatusInt >= 1{
                let vc = AuthenPendingVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }else{
                MCToast.mc_text("请先完成实名认证！")
            }
        }
        vm.funcThreeVm.tapBlock = {()in
            if UserModel.shared.verifyStatusInt >= 1{
                let vc = InviteVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }else{
                MCToast.mc_text("请先完成实名认证！")
            }
        }
        return vm
    }()
    lazy var dataFuncVm : MainFuncVM = {
        let vm = MainFuncVM.init(frame: CGRect.init(x: 0, y: self.zhanYeFuncVm.frame.maxY+kFitWidth(10), width: 0, height: 0))
        vm.updateUIForData()
        vm.funcOneVm.tapBlock = {()in
            if UserModel.shared.verifyStatusInt >= 1{
                let vc = ServiceMercPlatFormVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }else{
                MCToast.mc_text("请先完成实名认证！")
            }
        }
        vm.funcTwoVm.tapBlock = {()in
            if UserModel.shared.verifyStatusInt >= 1{
                let vc = MerchantManageVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }else{
                MCToast.mc_text("请先完成实名认证！")
            }
        }
        vm.funcThreeVm.tapBlock = {()in
            if UserModel.shared.verifyStatusInt >= 1{
                let vc = ServiceMercManageVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }else{
                MCToast.mc_text("请先完成实名认证！")
            }
        }
        return vm
    }()
    lazy var walletFuncVm : MainFuncVM = {
        let vm = MainFuncVM.init(frame: CGRect.init(x: 0, y: self.dataFuncVm.frame.maxY+kFitWidth(10), width: 0, height: 0))
        vm.updateUIForWallet()
        vm.funcOneVm.tapBlock = {()in
            let vc = RebateVC()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
}

extension MainVC{
    func initUI() {
        initNavi(titleStr: "星渝付助手")
        self.naviBackImg.isHidden = true
        self.navigationView.isUserInteractionEnabled = false
        
        view.addSubview(scrollView)
        scrollView.frame = CGRect.init(x: 0, y: self.getNavigationBarHeight(), width: SCREEN_WIDHT, height: SCREEN_HEIGHT-self.getNavigationBarHeight()-self.getTabbarHeight())
        scrollView.addSubview(bannerView)
        scrollView.addSubview(zhanYeFuncVm)
        scrollView.addSubview(dataFuncVm)
        
        if UserModel.shared.isCheck == false{
            scrollView.addSubview(walletFuncVm)
            scrollView.contentSize = CGSize.init(width: 0, height: self.walletFuncVm.frame.maxY+kFitWidth(20))
        }else{
            scrollView.contentSize = CGSize.init(width: 0, height: self.dataFuncVm.frame.maxY+kFitWidth(20))
        }
    }
}

extension MainVC{
    func sendGetVersionRequest(){
        WHNetworkUtil.shareManager().POST(urlString: URL_Get_Appversion, parameters: nil) { response in
            DLLog(message: "sendGetVersionRequest：\(response)")
            let dataDict = response["data"]as? NSDictionary ?? [:]
            let currentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
            DLLog(message: "currentBuildVersion:\(currentVersion)")
            
            let iosVersionNo = dataDict["iosVersionNo"]as? String ?? ""
            let iosVersionNoString = iosVersionNo.replacingOccurrences(of: ".", with: "")
//            let currentBuildInt = currentVersionString.intValue
            let iosVersionInt = iosVersionNoString.intValue
            
            //获取app信息
            let infoDictionary : Dictionary = Bundle.main.infoDictionary!
            //版本号
            let buildVersion :String = infoDictionary ["CFBundleVersion"] as! String
            let currentBuildInt = buildVersion.intValue
            
            if dataDict["iosIfUpdate"]as? String ?? "" == "1" && iosVersionInt > currentBuildInt{
                self.presentAlertVc(confirmBtn: "确定", message: dataDict["remark"]as? String ?? "", title: "V\(dataDict["iosVersionNo"]as? String ?? "") 版本更新", cancelBtn: nil, handler: { action in
                    self.openUrl(urlString: dataDict["iosDownUrl"]as? String ?? "")
//                    exit(0)
                }, viewController: self)
            }
        }
    }
    
    func sendGetSampleImgRequest()  {
        WHNetworkUtil.shareManager().POST(urlString: URL_Get_SampleImage, parameters: nil) { responseObject in
            DLLog(message: "sendGetSampleImgRequest:\(responseObject)")
            let datadict = responseObject["data"]as? NSDictionary ?? [:]
            AuthenSampleImageModel.shareInstance.dealSampleImg(dict: datadict)
        }
    }
}
